<?php
/**
 *  Flvmeister
 *  a multi-format video transcoder class written in PHP5 
 *  Copyright (C) 2008  Marco Fischer <marco@flvmeister.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by    
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *
 */

/**
 * Flvmeister class
 *
 * @author Marco Fischer <marco@flvmeister.com>
 * @copyright Copyright (C) 2008  Marco Fischer <marco@flvmeister.com>
 * @package Flvmeister
 * @version 0.1-dev
 */
class Flvmeister
{
    var $_encoder = null;

    const STATUS_NEW      = 'NEW';
    const STATUS_FINISHED = 'FINISHED';
    const STATUS_ERROR    = 'ERROR';
    
    function __construct( Flvmeister_Encoder_Abstract $encoder )
    {
        if ( !$encoder instanceof Flvmeister_Encoder_Abstract )
        {
            throw new Flvmeister_Exception( 'The given encoder has no Flvmeister_Encoder_Abstract' );
        } else
        {
            $this->_encoder = $encoder;
        }
    }

    public function addCronjob( Flvmeister_Job $job )
    {
        $db = $this->getDb( new Flvmeister_Config_Env() );
        
        $sFlvmeister = serialize( $this );
        $sJob        = serialize( $job );

        $db->insert( 'flvmeister_cronjobs', array( 'flvmeister' => $sFlvmeister, 
                                                   'job'        => $sJob,
                                                   'status'     => Flvmeister::STATUS_NEW
                                                 ));
    }
    
    public function getDb( Flvmeister_Config_Env $env )
    {
            return  Zend_Db::factory( 'Pdo_Mysql', array( 'host'     => $env->hostname,    
                                                               'username' => $env->username,    
                                                               'password' => $env->password,    
                                                               'dbname'   => $env->database
                                                             ));
    }
    
    private function _saveThumbnail( Flvmeister_Config_Thumbnail $tConfig )
    {
        return $this->_encoder->saveThumbnail( $tConfig );
    }
    
    private function _encodeVideo( Flvmeister_Config_Video $vConfig )
    {
        return $this->_encoder->encodeVideo( $vConfig );
    }
    
    public function worker( Flvmeister_Job $job )
    {
        foreach ($job->thumbnail as $tJob)
        {
            if ( !$this->_saveThumbnail( $tJob ) )
            {
                return false;
            }
        }
        
        foreach ($job->video as $vJob) 
        {
            if ( !$this->_encodeVideo( $vJob ) )
            {
                return false;
            }	
        }
        
        return true;        
    }
}
